package cn.sumpu.app.wlcity.web.util;

import java.io.File;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.joran.spi.JoranException;

public class LogbackConfigListener implements ServletContextListener {
	private static final Logger logger = LoggerFactory
			.getLogger(LogbackConfigListener.class);

	private static final String CONFIG_LOCATION = "logbackConfigLocation";

	public void contextInitialized(ServletContextEvent event) {
		String logbackConfigLocation = event.getServletContext()
				.getInitParameter(CONFIG_LOCATION);
		String fn = event.getServletContext().getRealPath("/") + File.separator
				+ "WEB-INF" + File.separator + "classes" + File.separator
				+ logbackConfigLocation;
		try {
			LoggerContext loggerContext = (LoggerContext) LoggerFactory
					.getILoggerFactory();
			loggerContext.reset();
			JoranConfigurator joranConfigurator = new JoranConfigurator();
			joranConfigurator.setContext(loggerContext);
			joranConfigurator.doConfigure(fn);
			if (logger.isDebugEnabled()) {
				logger.debug("Loaded slf4j configure file from {}", fn);
			}
		} catch (JoranException e) {
			if (logger.isErrorEnabled()) {
				logger.error("Can't loading slf4j configure file from " + fn, e);
			}
		}
	}

	public void contextDestroyed(ServletContextEvent sce) {
	}

}
